#!/bin/bash

# 字母转大写
case "$1" in
    "master")
        STATE=$(echo $1 | tr 'a-z' 'A-Z') 
    ;;
    "backup")
        STATE=$(echo $1 | tr 'a-z' 'A-Z') 
    ;;
    *)
    echo "错误：未传递文件参数错误！"
    echo "用法：keepalive.sh [master|backup] vip"
    echo "用法：keepalive.sh master 192.168.56.200"
    exit
    ;;
esac

# 网卡名+ip
source ../linux/ip.sh
echo "$NET_NAME=$IP"

# 下载位置https://www.keepalived.org/download.html
rm -rf /opt/module/keepalive/keepalived-2.0.20
# 开始下载
URL=https://www.keepalived.org/software/keepalived-2.0.20.tar.gz
source ../linux/download_unzip.sh "keepalive" $URL

#============== 调用本脚本时可用变量 ==============
#>>> $FILE_PATH 文件绝对路径：/opt/software/keepalived-2.0.20.tar.gz
#>>> $FILE_EXTENSION 文件后缀：gz
#>>> $File_HOME 软件HOME：/opt/module/keepalive/keepalived-2.0.20
#>>> $REPO_PATH 仓库目录：/opt/repository/keepalive

yum -y install libnl libnl-devel gcc openssl-devel

cd /opt/module/keepalive/keepalived-2.0.20

./configure --prefix=$File_HOME
cd /opt/module/keepalive/keepalived-2.0.20
make && make install


#====================================================
#                 系统命令、开机服务
#====================================================
# 拷贝执行文件
cp $File_HOME/sbin/keepalived /usr/sbin/
# 将初始化脚本拷贝到系统初始化目录下
cp $File_HOME/keepalived/etc/init.d/keepalived /etc/init.d/
# 将keepalived配置文件拷贝到etc下
cp $File_HOME/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# 创建keepalived文件夹
mkdir /etc/keepalived/
# 将keepalived配置文件拷贝到etc下
cp $File_HOME/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# 添加可执行权限
chmod +x /etc/init.d/keepalived
# 添加keepalived到开机启动
chkconfig --add keepalived
chkconfig keepalived on

# 复制【检测，通知脚本】
cp check_alive.sh /opt/module/keepalive
cp notify.sh /opt/module/keepalive

sudo cat >/etc/keepalived/keepalived.conf<<EOF
vrrp_script check_alive {
    script "/opt/module/keepalive/check_alive.sh"
    interval 1    #每隔1秒执行上述的脚本，检查ngnix状态
    weight -2     #宕机一次将自身权重减2
}

vrrp_instance VI_1 {
    state $STATE          //MASTER主节点，备用节点上设置为state BACKUP
    interface $NET_NAME        //绑定虚拟机IP的网卡  两个节点设置一样 根据 ipaddr换成对应的网卡地址
    virtual_router_id 51  //VRRP组名，主副节点设置必须一样，指名各个节点属于同一个VRRP组，同一个组的节点互相抢IP
    priority 100         //优先级(1~254之间)，备用节点必须比主节点优先级低
    advert_int 1         //组播信息发送间隔，两个节点设置必须一样
    authentication {    //设置验证信息， 两个节点设置必须一样，用于节点间信息转发时的加密
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {  // 虚拟IP两个节点设置必须一样，两节点同时抢一个io
        192.168.56.200/24  // 如果两个nginx的ip分别是192.168.33.61,,...62，则此处的虚拟ip跟它俩同一个网段即可 24代表3个255的子网掩码
    }
        # 新增检测Nginx脚本 自定义脚本名check_nginx_alive
    track_script {
        check_alive
    }

    # keepalived 三个接口，当状态改变时执行用户自定义脚本
    notify_master "/opt/module/keepalive/notify.sh master"
    notify_backup "/opt/module/keepalive/notify.sh backup"
    notify_fault "/opt/module/keepalive/notify.sh fault"
}
EOF

# #启动
service keepalived restart
# #停止
# service keepalived stop
# #重启
# service keepalived restart
# #查看启动情况
# ps -aux |grep keepalived


yum install -y tcpdump
# 开始监测
tcpdump -i enp0s8 vrrp -n
# 参考
# https://www.jianshu.com/p/fae5e3252fc8


